package cn.yuan.xiaoyu.chatmodule.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.chatmodule.bean.SendMessageBean;
import cn.yuan.xiaoyu.commonmodule.config.Constant;

/**
 * Created by yukuo on 2016/3/5.
 * 这是一个聊天界面的适配器
 */
public class ChartListVIewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SendMessageBean> list;
    private Intent intent;

    public ChartListVIewAdapter(List<SendMessageBean> list) {
        super();
        this.list = list;

    }

    /**
     * 这是一个添加一条数据并刷新界面的方法
     *
     * @param msg
     */
    public void addData(SendMessageBean msg) {
        list.add(list.size(), msg);
        notifyItemInserted(list.size());
    }


    @Override
    public int getItemViewType(int position) {
        int type = list.get(position).result.type;
        if (type == 1) {
            return Constant.SENDTXT;
        } else if (type == 0) {
            return Constant.FROMTXT;
        }

        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == Constant.SENDTXT) {
            //发送者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_send_txt, parent, false);
            return new SendTxtVIewHolder(view);
        } else if (viewType == Constant.FROMTXT) {
            //接受者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_from_txt, parent, false);
            return new FromTxtVIewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SendTxtVIewHolder) {
            String content = list.get(position).result.text;
            ((SendTxtVIewHolder) holder).tv_item_send_txt.setText(content);
        } else if (holder instanceof FromTxtVIewHolder) {
            String content = list.get(position).result.text;
            ((FromTxtVIewHolder) holder).tv_item_from_txt.setText(content);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class SendTxtVIewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_send_txt;

        public SendTxtVIewHolder(View view) {
            super(view);
            tv_item_send_txt = (TextView) view.findViewById(R.id.tv_item_send_txt);
        }
    }


    class FromTxtVIewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_from_txt;

        public FromTxtVIewHolder(View view) {
            super(view);
            tv_item_from_txt = (TextView) view.findViewById(R.id.tv_item_from_txt);
        }
    }

}
